<?php
namespace App\DBs;

use Illuminate\Support\Facades\DB;

class ImgDB extends BaseDB
{
    /**
     * IMG
     * admin 管理员UID
     * genre 1BUG表，2BUG处理表
     * fromid 来源表的ID
     * link 链接
     * del 0未删除，1已删除，
     */

    protected $table = 'img';

    public function __construct()
    {
        parent::__construct();
    }

    public function getGenres()
    {
        return array(
            1 => 'BUG表', 'BUG处理表',
        );
    }

    /**
     * 判断图片
     */
    public function isImg($adminId,$genre,$fromid)
    {
        $models = DB::table($this->table)
            ->where('del',0)
            ->where('admin',$adminId)
            ->where('genre',$genre)
            ->where('fromid',$fromid)
            ->orderBy('id','asc')
            ->get();
        if (!count($models)) { return false; }
        return true;
    }

    /**
     * 获取图片
     */
    public function getIndex($adminId,$genre,$fromid)
    {
        $models = DB::table($this->table)
            ->where('del',0)
            ->where('admin',$adminId)
            ->where('genre',$genre)
            ->where('fromid',$fromid)
            ->orderBy('id','asc')
            ->get();
        $dataArr = array();
        if (!count($models)) { return $dataArr; }
        $genres = $this->getGenres();
        foreach ($models as $model) {
            $genreName = array_key_exists($model->genre,$genres)
                ? $genres[$model->genre] : '';
            $createTime = $model->created_at ? $model->created_at : '未定义';
            $dataArr[] = [
                'adminId' => $adminId,
                'genre' => $model->genre,
                'genreName' => $genreName,
                'fromid' => $model->fromid,
                'link' => $model->link,
                'createTime' => $createTime,
            ];
        }
        return $dataArr;
    }
}